﻿using System.Text;
using System.Web;

namespace ECommon
{
    /// <summary>
    /// 字符编码工具
    /// </summary>
    public static class EncodeTool
    {
        /// <summary>
        /// ascii码转换为base64编码
        /// </summary>
        /// <param name="sourceString">ascii码字符串</param>
        /// <returns>base64编码字符串</returns>
        public static string ASCIIToBase64(string sourceString)
        {
            return ConvertToBase64(sourceString, Encoding.ASCII);
        }

        /// <summary>
        /// ascii码转换为指定编码
        /// </summary>
        /// <param name="sourceString">ascii码字符串</param>
        /// <param name="encoding">指定编码</param>
        /// <returns>指定编码字符串</returns>
        public static string ASCIIToConvert(string sourceString, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(sourceString))
            {
                return sourceString;
            }
            if (encoding == Encoding.ASCII)
            {
                return sourceString;
            }
            return encoding.GetString(Encoding.ASCII.GetBytes(sourceString));
        }

        /// <summary>
        /// ascii码转换为默认编码
        /// </summary>
        /// <param name="sourceString">ascii码字符串</param>
        /// <returns>默认编码字符串</returns>
        public static string ASCIIToDefault(string sourceString)
        {
            return ASCIIToConvert(sourceString, Encoding.Default);
        }

        /// <summary>
        /// ascii码转换为gb2312编码字符串
        /// </summary>
        /// <param name="sourceString">ascii码字符串</param>
        /// <returns>gb2312编码字符串</returns>
        public static string ASCIIToGB2312(string sourceString)
        {
            return ASCIIToConvert(sourceString, Encoding.GetEncoding("GB2312"));
        }

        /// <summary>
        /// ascii码转换为url编码字符串
        /// </summary>
        /// <param name="sourceString">ascii码字符串</param>
        /// <returns>url编码字符串</returns>
        public static string ASCIIToUrl(string sourceString)
        {
            return ConvertToUrl(sourceString, Encoding.ASCII);
        }

        /// <summary>
        /// ascii码转换为utf8编码字符串
        /// </summary>
        /// <param name="sourceString">ascii码字符串</param>
        /// <returns>utf8编码字符串</returns>
        public static string ASCIIToUTF8(string sourceString)
        {
            return ASCIIToConvert(sourceString, Encoding.UTF8);
        }

        /// <summary>
        /// base64编码字符串转换为ascii码字符串
        /// </summary>
        /// <param name="sourceString">base64编码字符串</param>
        /// <returns>ascii码字符串</returns>
        public static string Base64ToASCII(string sourceString)
        {
            return Base64ToConvert(sourceString, Encoding.ASCII);
        }

        /// <summary>
        /// base64编码字符串转换为指定编码字符串
        /// </summary>
        /// <param name="sourceString">base64编码字符串</param>
        /// <param name="encoding">指定编码</param>
        /// <returns>指定编码字符串</returns>
        public static string Base64ToConvert(string sourceString, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(sourceString))
            {
                return sourceString;
            }
            return encoding.GetString(Convert.FromBase64String(sourceString));
        }

        /// <summary>
        /// base64编码字符串转换为默认编码字符串
        /// </summary>
        /// <param name="sourceString">base64编码字符串</param>
        /// <returns>默认编码字符串</returns>
        public static string Base64ToDefault(string sourceString)
        {
            return Base64ToConvert(sourceString, Encoding.Default);
        }

        /// <summary>
        /// base64编码字符串转换为gb2312编码字符串
        /// </summary>
        /// <param name="sourceString">base64编码字符串</param>
        /// <returns>gb2312编码字符串</returns>
        public static string Base64ToGB2312(string sourceString)
        {
            return Base64ToConvert(sourceString, Encoding.GetEncoding("GB2312"));
        }

        /// <summary>
        /// base64编码字符串转换为url编码字符串
        /// </summary>
        /// <param name="sourceString">base64编码字符串</param>
        /// <param name="middleEncoding"></param>
        /// <returns>url编码字符串</returns>
        public static string Base64ToUrl(string sourceString, Encoding middleEncoding)
        {
            return ConvertToUrl(middleEncoding.GetString(Convert.FromBase64String(sourceString)), middleEncoding);
        }

        /// <summary>
        /// base64编码字符串转换为utf8编码字符串
        /// </summary>
        /// <param name="sourceString">base64编码字符串</param>
        /// <returns>utf8编码字符串</returns>
        public static string Base64ToUTF8(string sourceString)
        {
            return Base64ToConvert(sourceString, Encoding.UTF8);
        }

        /// <summary>
        /// 字符串转换为指定编码字符串
        /// </summary>
        /// <param name="sourceString">字符串</param>
        /// <param name="encoding">指定编码</param>
        /// <returns>指定编码字符串</returns>
        public static string ConvertToBase64(string sourceString, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(sourceString))
            {
                return sourceString;
            }
            return Convert.ToBase64String(encoding.GetBytes(sourceString));
        }

        /// <summary>
        /// 字符串转换为根据指定编码转换为url编码字符串
        /// </summary>
        /// <param name="sourceString">字符串</param>
        /// <param name="encoding">指定编码</param>
        /// <returns>url编码字符串</returns>
        public static string ConvertToUrl(string sourceString, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(sourceString))
            {
                return sourceString;
            }
            return HttpUtility.UrlEncode(encoding.GetBytes(sourceString));
        }

        /// <summary>
        /// 转换默认编码字符串为ascii码字符串
        /// </summary>
        /// <param name="sourceString">默认编码字符串</param>
        /// <returns>ascii码字符串</returns>
        public static string DefaultToASCII(string sourceString)
        {
            return DefaultToConvert(sourceString, Encoding.ASCII);
        }

        /// <summary>
        /// 转换默认编码字符串为base64编码字符串
        /// </summary>
        /// <param name="sourceString">默认编码字符串</param>
        /// <returns>base64编码字符串</returns>
        public static string DefaultToBase64(string sourceString)
        {
            return ConvertToBase64(sourceString, Encoding.Default);
        }

        /// <summary>
        /// 转换默认编码字符串为指定编码字符串
        /// </summary>
        /// <param name="sourceString">默认编码字符串</param>
        /// <param name="encoding">指定编码</param>
        /// <returns>指定编码字符串</returns>
        public static string DefaultToConvert(string sourceString, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(sourceString))
            {
                return sourceString;
            }
            if (encoding == Encoding.Default)
            {
                return sourceString;
            }
            return encoding.GetString(Encoding.Default.GetBytes(sourceString));
        }

        /// <summary>
        /// 转换默认编码字符串为gb2312编码字符串
        /// </summary>
        /// <param name="sourceString">默认编码字符串</param>
        /// <returns>gb2312编码字符串</returns>
        public static string DefaultToGB2312(string sourceString)
        {
            return DefaultToConvert(sourceString, Encoding.GetEncoding("GB2312"));
        }

        /// <summary>
        /// 转换默认编码字符串为url编码字符串
        /// </summary>
        /// <param name="sourceString">默认编码字符串</param>
        /// <returns>url编码字符串</returns>
        public static string DefaultToUrl(string sourceString)
        {
            return ConvertToUrl(sourceString, Encoding.Default);
        }

        /// <summary>
        /// 转换默认编码字符串为utf8编码字符串
        /// </summary>
        /// <param name="sourceString">默认编码字符串</param>
        /// <returns>utf8编码字符串</returns>
        public static string DefaultToUTF8(string sourceString)
        {
            return DefaultToConvert(sourceString, Encoding.UTF8);
        }

        /// <summary>
        /// 转换gb2312编码字符串为ascii码字符串
        /// </summary>
        /// <param name="sourceString">gb2312编码字符串</param>
        /// <returns>ascii码字符串</returns>
        public static string GB2312ToASCII(string sourceString)
        {
            return GB2312ToConvert(sourceString, Encoding.ASCII);
        }

        /// <summary>
        /// 转换gb2312编码字符串为base64编码字符串
        /// </summary>
        /// <param name="sourceString">gb2312编码字符串</param>
        /// <returns>base64编码字符串</returns>
        public static string GB2312ToBase64(string sourceString)
        {
            return ConvertToBase64(sourceString, Encoding.GetEncoding("GB2312"));
        }

        /// <summary>
        /// 转换gb2312编码字符串为指定编码字符串
        /// </summary>
        /// <param name="sourceString">gb2312编码字符串</param>
        /// <param name="encoding">指定编码</param>
        /// <returns>指定编码字符串</returns>
        public static string GB2312ToConvert(string sourceString, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(sourceString))
            {
                return sourceString;
            }
            if (encoding == Encoding.GetEncoding("GB2312"))
            {
                return sourceString;
            }
            return encoding.GetString(Encoding.GetEncoding("GB2312").GetBytes(sourceString));
        }

        /// <summary>
        /// 转换gb2312编码字符串为默认编码字符串
        /// </summary>
        /// <param name="sourceString">gb2312编码字符串</param>
        /// <returns>默认编码字符串</returns>
        public static string GB2312ToDefault(string sourceString)
        {
            return GB2312ToConvert(sourceString, Encoding.Default);
        }

        /// <summary>
        /// 转换gb2312编码字符串url编码字符串
        /// </summary>
        /// <param name="sourceString">gb2312编码字符串</param>
        /// <returns>url编码字符串</returns>
        public static string GB2312ToUrl(string sourceString)
        {
            return ConvertToUrl(sourceString, Encoding.GetEncoding("GB2312"));
        }

        /// <summary>
        /// 转换gb2312编码字符串为utf8编码字符串
        /// </summary>
        /// <param name="sourceString">gb2312编码字符串</param>
        /// <returns>utf8编码字符串</returns>
        public static string GB2312ToUTF8(string sourceString)
        {
            return GB2312ToConvert(sourceString, Encoding.UTF8);
        }

        /// <summary>
        /// 转换url编码字符串为ascii码字符串
        /// </summary>
        /// <param name="sourceString">url编码字符串</param>
        /// <returns>ascii码字符串</returns>
        public static string UrlToASCII(string sourceString)
        {
            return UrlToConvert(sourceString, Encoding.ASCII);
        }

        /// <summary>
        /// 转换url编码字符串为base64编码字符串
        /// </summary>
        /// <param name="sourceString">url编码字符串</param>
        /// <returns>base64编码字符串</returns>
        public static string UrlToBase64(string sourceString)
        {
            return ConvertToBase64(HttpUtility.UrlDecode(sourceString, Encoding.UTF8), Encoding.UTF8);
        }

        /// <summary>
        /// 转换url编码字符串为指定编码字符串
        /// </summary>
        /// <param name="sourceString">url编码字符串</param>
        /// <param name="encoding">指定编码</param>
        /// <returns>指定编码字符串</returns>
        public static string UrlToConvert(string sourceString, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(sourceString))
            {
                return sourceString;
            }
            return HttpUtility.UrlDecode(sourceString, encoding);
        }

        /// <summary>
        /// 转换url编码字符串为默认编码字符串
        /// </summary>
        /// <param name="sourceString">url编码字符串</param>
        /// <returns>默认编码字符串</returns>
        public static string UrlToDefault(string sourceString)
        {
            return UrlToConvert(sourceString, Encoding.Default);
        }

        /// <summary>
        /// 转换url编码字符串为gb2312编码字符串
        /// </summary>
        /// <param name="sourceString">url编码字符串</param>
        /// <returns>gb2312编码字符串</returns>
        public static string UrlToGB2312(string sourceString)
        {
            return UrlToConvert(sourceString, Encoding.GetEncoding("GB2312"));
        }

        /// <summary>
        /// 转换url编码字符串为utf8编码字符串
        /// </summary>
        /// <param name="sourceString">url编码字符串</param>
        /// <returns>utf8编码字符串</returns>
        public static string UrlToUTF8(string sourceString)
        {
            return UrlToConvert(sourceString, Encoding.UTF8);
        }

        /// <summary>
        /// 转换utf8编码字符串为ascii码字符串
        /// </summary>
        /// <param name="sourceString">utf8编码字符串</param>
        /// <returns>ascii码字符串</returns>
        public static string UTF8ToASCII(string sourceString)
        {
            return UTF8ToConvert(sourceString, Encoding.ASCII);
        }

        /// <summary>
        /// 转换utf8编码字符串为base64编码字符串
        /// </summary>
        /// <param name="sourceString">utf8编码字符串</param>
        /// <returns>base64编码字符串</returns>
        public static string UTF8ToBase64(string sourceString)
        {
            return ConvertToBase64(sourceString, Encoding.UTF8);
        }

        /// <summary>
        /// 转换utf8编码字符串为指定编码字符串
        /// </summary>
        /// <param name="sourceString">utf8编码字符串</param>
        /// <param name="encoding">指定编码</param>
        /// <returns>指定编码字符串</returns>
        public static string UTF8ToConvert(string sourceString, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(sourceString))
            {
                return sourceString;
            }
            if (encoding == Encoding.UTF8)
            {
                return sourceString;
            }
            return encoding.GetString(Encoding.UTF8.GetBytes(sourceString));
        }

        /// <summary>
        /// 转换utf8编码字符串为默认编码字符串
        /// </summary>
        /// <param name="sourceString">utf8编码字符串</param>
        /// <returns>默认编码字符串</returns>
        public static string UTF8ToDefault(string sourceString)
        {
            return UTF8ToConvert(sourceString, Encoding.Default);
        }

        /// <summary>
        /// 转换utf8编码字符串为gb2312编码字符串
        /// </summary>
        /// <param name="sourceString">utf8编码字符串</param>
        /// <returns>gb2312编码字符串</returns>
        public static string UTF8ToGB2312(string sourceString)
        {
            return UTF8ToConvert(sourceString, Encoding.GetEncoding("GB2312"));
        }

        /// <summary>
        /// 转换utf8编码字符串为url编码字符串
        /// </summary>
        /// <param name="sourceString">utf8编码字符串</param>
        /// <returns>url编码字符串</returns>
        public static string UTF8ToUrl(string sourceString)
        {
            return ConvertToUrl(sourceString, Encoding.UTF8);
        }
    }
}